

package cn.shengchao.examstar.share.acl.adapters.repositories;

import cn.shengchao.examstar.share.acl.ports.repositories.IShareLinkRepository;
import cn.shengchao.examstar.share.infrastructure.dao.ShareLinkDao;
import cn.shengchao.examstar.share.infrastructure.dao.ShareLinkLogDao;
import cn.shengchao.examstar.share.infrastructure.dao.po.TShareLink;
import cn.shengchao.examstar.share.domain.entity.ShareLink;
import cn.shengchao.examstar.share.domain.valobj.Product;
import cn.shengchao.examstar.share.domain.valobj.User;
import cn.shengchao.examstar.share.infrastructure.dao.po.TShareLinkLog;
import cn.shengchao.examstart.user.acl.ports.repositories.IUserRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class ShareLinkRepository implements IShareLinkRepository {

    @Autowired
    private ShareLinkDao shareLinkDao;

    @Autowired
    private ShareLinkLogDao shareLinkLogDao;

    @Autowired
    private IUserRepository iUserRepository;

    @Override
    public void save(ShareLink shareLink) {
        TShareLink tShareLink = new TShareLink();
        if (shareLink.getProduct() != null) {
            tShareLink.setProductId(shareLink.getProduct().getId());
        }
        tShareLink.setShareCode(shareLink.getShareCode());
        tShareLink.setShareUser(shareLink.getShareUser().getId());
        tShareLink.setClickSum(0L);
        tShareLink.setCreateUser(shareLink.getShareUser().getId());
        tShareLink.setUpdateUser(shareLink.getShareUser().getId());
        tShareLink.setPath(shareLink.getPath());
        tShareLink.setTitle(shareLink.getTitle());
        shareLinkDao.insert(tShareLink);
    }

    @Override
    public Optional<ShareLink> getShareLinkByProductIdAndShareUserId(Long productId, Long userId) {
        TShareLink tShareLink = shareLinkDao.selectOne(new LambdaQueryWrapper<TShareLink>()
                .eq(TShareLink::getProductId, productId)
                .eq(TShareLink::getShareUser, userId));
        return getShareLink(tShareLink);
    }

    @Override
    public Optional<ShareLink> getShareLinkByShareCode(String shareCode) {
        TShareLink tShareLink = shareLinkDao.selectOne(new LambdaQueryWrapper<TShareLink>()
                .eq(TShareLink::getShareCode, shareCode));
        return getShareLink(tShareLink);
    }

    @NotNull
    private Optional<ShareLink> getShareLink(TShareLink tShareLink) {
        if (tShareLink == null) {
            return Optional.empty();
        }
        ShareLink shareLink = new ShareLink();
        shareLink.setId(tShareLink.getId());
        shareLink.setShareCode(tShareLink.getShareCode());
        cn.shengchao.examstart.user.domain.entity.User user = iUserRepository.of(tShareLink.getShareUser());
        User shareUser=new User();
        shareUser.setId(user.getId());
        shareUser.setNickName(user.getName());
        shareUser.setAvatar(user.getAvatar());
        shareLink.setShareUser(shareUser);
        Product product = new Product();
        product.setId(tShareLink.getProductId());
        shareLink.setProduct(product);
        return Optional.of(shareLink);
    }

    @Override
    public void addLog(ShareLink shareLink, User clickUser) {
        TShareLinkLog tShareLinkLog = new TShareLinkLog();
        tShareLinkLog.setShareLinkId(shareLink.getId());
        tShareLinkLog.setClickUser(clickUser.getId());
        tShareLinkLog.setShareUserId(shareLink.getShareUser().getId());
        tShareLinkLog.setProductId(shareLink.getProduct().getId());
        shareLinkLogDao.insert(tShareLinkLog);
    }
}
